import { _HttpClient } from '@delon/theme';
import { QueryOptions } from 'src/app/project/model/common/query-options';
import { Observable } from 'rxjs';
import { ApiPagedData } from 'src/app/project/model/common/api-paged-data';
import { TeacherDTO } from 'src/app/project/model/icm/icm-system-user/teacher/teacher.dto';
import { ApiSimpleData } from 'src/app/project/model/common/api-simple-data';
import { Injectable } from '@angular/core';

@Injectable({
  providedIn: 'root',
})
export class TeacherSerivce {
  private static URL = '/api/icm-teacher';

  constructor(private http: _HttpClient) {}

  /**
   * 分页查询教师信息
   * @param option 分页参数
   * @param academyId 所属学院id
   * @param code 教师编号
   * @param name 教师名称
   */
  findOnePage(
    option: QueryOptions,
    academyId?: string,
    code?: string,
    name?: string,
  ): Observable<ApiPagedData<TeacherDTO>> {
    const params = {};
    Object.assign(params, option, academyId ? { academyId } : {}, code ? { code } : {}, name ? { name } : {});
    return this.http.get<ApiPagedData<TeacherDTO>>(TeacherSerivce.URL, params);
  }

  /**
   * 添加教师
   * @param teacherDTO 教师信息
   */
  create(teacherDTO: TeacherDTO): Observable<TeacherDTO> {
    return this.http.post<TeacherDTO>(TeacherSerivce.URL, teacherDTO);
  }

  /**
   * 根据id查找教师
   * @param id 教师id
   */
  findById(id: string): Observable<TeacherDTO> {
    return this.http.get<TeacherDTO>(`${TeacherSerivce.URL}/${id}`);
  }

  /**
   * 修改更新教师信息
   * @param id 教师id
   * @param teacherDTO 教师信息
   */
  complete(id: string, teacherDTO: TeacherDTO): Observable<TeacherDTO> {
    return this.http.put<TeacherDTO>(`${TeacherSerivce.URL}/${id}`, teacherDTO);
  }

  /**
   * 根据id删除教师
   * @param id 教师id
   */
  delete(id: string): Observable<TeacherDTO> {
    return this.http.delete<TeacherDTO>(`${TeacherSerivce.URL}/${id}`);
  }

  /**
   * 查找所有教师，可按需求排序
   * @param param 排序参数
   * @param pattern 排序模式: asc/desc
   */
  findAllBySort(param?: string, pattern?: string): Observable<Array<TeacherDTO>> {
    const params = {};
    Object.assign(params, param ? { param } : {}, pattern ? { pattern } : {});
    return this.http.get<Array<TeacherDTO>>(`${TeacherSerivce.URL}/all`, params);
  }

  /**
   * 根据教师code，检查教师code是否已存在
   * @param code 教师code
   */
  checkoutCodeIsExist(code: string): Observable<ApiSimpleData<boolean>> {
    const params = {};
    Object.assign(params, code ? { code } : {});
    return this.http.get<ApiSimpleData<boolean>>(`${TeacherSerivce.URL}/checkCodeExist`, params);
  }

  /**
   * 根据userId查找教师
   * @param userId userId
   */
  findByUserId(userId: string): Observable<TeacherDTO> {
    return this.http.get<TeacherDTO>(`${TeacherSerivce.URL}/byUser/${userId}`);
  }
}
